list1 = [1,2,3,4,5]
for i in list1:
if i ==3:
continue
print(i)
1 2 4 5
n = 20
while n >= 1:
if n % 2 != 0:
print(n)
n -= 1
19 17 15 13 11 9 7 5 3 1
num = 10
while num >= 5:
print(num)
num -= 1
10 9 8 7 6 5
i = 1
while i <= 8:
if i % 2 == 1:
print(i)
else:
print("*")
i += 1
1 * 3 * 5 * 7 *
i = 0
while i < 30:
i = i + 3
if i == 3 or i == 6:
continue
print(i)
9 12 15 18 21 24 27 30
i = 0
while i <= 21:
print(i)
i += 3
0 3 6 9 12 15 18 21
s = "Welcome"
for i in s:
print(i)
W e l c o m e
for i in range(10):
for j in range(10):
print(j,end=" ")
print()
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
for i in range(10):
for j in range(10):
print(i,end=" ")
print()
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9
for i in range(10):
for j in range(10):
print("*",end=" ")
print()
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
for i in range(1, 5):
for j in range(1, i + 1):
print(j, end=" ")
print()
1 1 2 1 2 3 1 2 3 4
n = 1
for i in range(1,6):
for j in range(1,6):
print(n,end=" ")
n += 1
print()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
n = 1
l1 =[1,2,3,4,5,6,7,8,9]
for i in range(1,6):
for j in range(1,6):
if n in l1:
print(n,end=" ")
else:
print(n,end=" ")
n += 1
print()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
n = 8
for i in range(n):
for j in range(i+1):
print("*",end=" ")
print()
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
n = 8
for i in range(n,0,-1):
for j in range(i):
print("*",end=" ")
print()
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
n = 8
for i in range(n):
for j in range(i+1):
print(i,end=" ")
print()
0 1 1 2 2 2 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7
n = 8
for i in range(n,0,-1):
for j in range(i):
print(i,end=" ")
print()
8 8 8 8 8 8 8 8 7 7 7 7 7 7 7 6 6 6 6 6 6 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
n = 5
x = 1
for i in range(n):
for j in range(i+1):
print(x,end=" ")
x += 1
print()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
n = 8
for i in range(n):
for j in range(n-i-1):
print(" ",end=" ")
for j in range(i+1):
print("*",end=" ")
print()
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
n = 8
for i in range(n):
for j in range(n-i-1):
print(" ",end=" ")
for j in range(i+1):
print("*",end=" ")
for j in range(i):
print("*",end=" ")
print()
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * * *
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
n = 8
if is_prime(n):
print(n, "is a prime number.")
else:
print(n, "is not a prime number.")
8 is not a prime number.
n = 11
if n <= 1:
print(n, "is not a prime number.")
else:
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
print(n, "is not a prime number.")
break
else:
print(n, "is a prime number.")
11 is a prime number.
n = 11
flag =0
for i in range(2,n):
if n%i == 0:
print("Not Prime")
flag = 1
break
if flag == 0:
print("Prime")
Prime
n = 5
factorial = 1
for i in range(1,n+1):
factorial = factorial*i
print(factorial)
120
n1= 0
n2 = 1
print(n1)
print(n2)
for i in range(0,20):
n3 = n1 + n2
n1 = n2
n2 = n3
print(n3)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946
nterms = 20
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
n = 9
n1 = 0
n2 = 1
count = 0
if n == 0:
print("0")
else:
while count < n:
print(n1)
n3 = n1 + n2
n1 = n2
n2 = n3
count += 1
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
n = 8
for i in range(n):
for j in range(n-i-1):
print(" ",end=" ")
for j in range(i+1):
print("*",end=" ")
for j in range(i):
print("*",end=" ")
print()
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * * *
def generate_pascals_triangle(n):
# Initialize an empty list to store the triangle
triangle = []
# Loop to generate each row of the triangle
for i in range(n):
# Initialize a list for the current row
row = []
# Calculate the binomial coefficients for this row
for j in range(i + 1):
# The first and last elements in each row are always 1
if j == 0 or j == i:
row.append(1)
else:
# Calculate the middle elements using the formula C(n, k) = C(n-1, k-1) + C(n-1, k)
coefficient = triangle[i - 1][j - 1] + triangle[i - 1][j]
row.append(coefficient)
# Add the current row to the triangle
triangle.append(row)
return triangle
def print_pascals_triangle(triangle):
max_width = len(" ".join(map(str, triangle[-1])))
# Loop to print each row of the triangle
for row in triangle:
row_str = " ".join(map(str, row))
# Center-align the row within the maximum width for a nicer display
print(row_str.center(max_width))
# Input: Number of rows for Pascal's Triangle
n = int(input("Enter the number of rows for Pascal's Triangle: "))
# Generate Pascal's Triangle
triangle = generate_pascals_triangle(n)
# Print Pascal's Triangle
print("Pascal's Triangle with", n, "rows:")
print_pascals_triangle(triangle)
Pascal's Triangle with 9 rows:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
triange = []
for i in range(6):
row = []
for j in range(i+1):
if j == 0 or j == i:
row.append(1)
else:
x = triange[i-1][j-1] + triange[i-1][j]
row.append(x)
triange.append(row)
print(triange)
n = len(triange)
print(n)
for i in range(n):
for j in range(n-1-i):
print(" ", end="")
for j in range(i + 1):
print(triange[i][j], end=" ")
print()
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1]]
6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
pascal triangle using power of 11
rows = 9
for r in range(rows):
print(' '*(rows-r), end='')
print(' '.join(map(str, str(11**r))))
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 6 1 0 5 1
1 7 7 1 5 6 1
1 9 4 8 7 1 7 1
2 1 4 3 5 8 8 8 1
def sum(a,b):
c = a + b
return c
d = sum(5,6)
print(d)
#print(c)
11
def test1():
a = 50
b = 80
print(a,b)
def test2():
a = 22
b = 44
print(a,b)
print(test1())
print(test2())
50 80 None 22 44 None
li =[1,2,3,4,5]
for i in li:
print(i,end=" ")
1 2 3 4 5
a = 5
def test():
global a
a = a + 1
return(a)
b = test()
print(b)
print(a)
a = a + 1
print(a)
6 6 7
def double(a):
return a * 2
def square(a):
return a * a
print("result",double(5))
print("result",square(5))
print("result",double(square(5)))
result 10 result 25 result 50
def triple(a):
return a * 3
def cube(a):
return a * 3
print(cube(triple(2)))
18
def fibonacci(n):
fib_series = []
a, b = 0, 1
while len(fib_series) < n:
fib_series.append(a)
a, b = b, a + b
return fib_series
# Change the value of 'n' to the number of terms you want to generate
n = 3 # You can change this value to generate a different number of terms
if n <= 0:
print("Please enter a positive integer.")
else:
result = fibonacci(n)
print("Fibonacci Series:")
print(result)
Fibonacci Series: [0, 1, 1]
armstrong
def is_armstrong(number):
# Convert the number to a string to count the number of digits
num_str = str(number)
# Calculate the number of digits (n)
n = len(num_str)
# Calculate the sum of digits raised to the nth power
sum_of_digits = sum(int(digit) ** n for digit in num_str)
# Check if the sum is equal to the original number
return sum_of_digits == number
# Input number to check
num = int(input("Enter a number: "))
# Check if it's an Armstrong number
if is_armstrong(num):
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
7777 is not an Armstrong number.
n = 777
x = str(n)
l = len(x)
sum = 0
for i in x:
p = int(i)**l
sum += p
print(sum)
if sum == n:
print("ha")
else:
print("na")
1029 na
n = 145
s = 0
st = str(n)
for i in st:
fact = 1
u = int(i)
for i in range(1,u+1):
fact *=i
s += fact
print(s)
145
A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself). An abundant number is a positive integer for which the sum of its proper divisors is greater than the number itself. A deficient number is a positive integer for which the sum of its proper divisors is less than the number itself.
Here's a Python program that determines if a given number is perfect, abundant, or deficient:
def type_of_num(num):
factors = []
for i in range(1, num + 1):
if num % i == 0:
if i != num:
factors.append(i)
if sum(factors) == num:
type_num = "Perfect"
elif sum(factors) > num:
type_num = "Abundant"
else:
type_num = "Deficient"
return type_num, factors
print(type_of_num(32))
('Deficient', [1, 2, 4, 8, 16])